import type { FolderProps } from '@/types';

import os from 'node:os';

/**
 * @description 是够有滚动条
 */
export const hasScrollbar = () => {
  return (
    document.body.scrollHeight >
    (window.innerHeight || document.documentElement.clientHeight)
  );
};

/**
 * @description 获得滚动条的宽度
 */
export const getScrollbarWidth = () => {
  const scrollDiv = document.createElement('div');

  scrollDiv.style.cssText =
    'width: 99px; height: 99px; overflow: scroll; position: absolute; top: -9999px;';
  document.body.append(scrollDiv);

  const scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;

  scrollDiv.remove();

  return scrollbarWidth;
};
/**
 * @description 格式化文件大小
 * @param size 文件大小
 * @return 格式化后的文件大小
 */
export const formatFileSize = (size: number) => {
  if (size < 1024) {
    return `${size} B`;
  } else if (size < 1024 * 1024) {
    return `${(size / 1024).toFixed(2)} KB`;
  } else if (size < 1024 * 1024 * 1024) {
    return `${(size / (1024 * 1024)).toFixed(2)} MB`;
  } else {
    return `${(size / (1024 * 1024 * 1024)).toFixed(2)} GB`;
  }
};
/**
 * @description 获取文件图标
 * @param item 文件信息
 * @return 文件图标
 */
export function formatIcon(item: FolderProps) {
  if (item.type === '2') {
    return 'fa6-solid:folder';
  } else {
    switch (item.ext) {
      case '.zip':
      case '.rar':
      case '.7z': {
        return 'vscode-icons:file-type-zip';
      }
      case '.pdf': {
        return 'vscode-icons:file-type-pdf2';
      }
      case '.doc': {
        return 'vscode-icons:file-type-word2';
      }
      case '.docx': {
        return 'vscode-icons:file-type-word';
      }
      case '.xls': {
        return 'vscode-icons:file-type-excel2';
      }
      case '.xlsx': {
        return 'vscode-icons:file-type-excel';
      }
      case '.ppt': {
        return 'vscode-icons:file-type-powerpoint2';
      }
      case '.pptx': {
        return 'vscode-icons:file-type-powerpoint';
      }
      case '.txt': {
        return 'vscode-icons:file-type-text';
      }
      case '.ts': {
        return 'vscode-icons:file-type-typescript-official';
      }
      case '.toml': {
        return 'vscode-icons:file-type-toml';
      }
      case '.config': {
        return 'vscode-icons:file-type-config';
      }
      case '.ini': {
        return 'vscode-icons:file-type-ini';
      }
      case '.js': {
        return 'vscode-icons:file-type-js-official';
      }
      case '.java': {
        return 'vscode-icons:file-type-jar';
      }
      case '.json': {
        return 'vscode-icons:file-type-json';
      }
      case '.py': {
        return 'vscode-icons:file-type-python';
      }
      case '.psd': {
        return 'devicon:photoshop';
      }
      case '.mp3': {
        return 'vscode-icons:file-type-audio';
      }
      case '.mp4': {
        return 'vscode-icons:file-type-video';
      }
      case '.jpg':
      case '.jpeg':
      case '.png':
      case '.gif':
      case '.bmp': {
        return 'vscode-icons:file-type-image';
      }
      case '.xml': {
        return 'vscode-icons:file-type-xml';
      }
      case '.svg': {
        return 'vscode-icons:file-type-svg';
      }
      case '.sql': {
        return 'vscode-icons:file-type-sql';
      }
      case '.css':
      case '.less':
      case '.scss':
      case '.sass': {
        return 'vscode-icons:file-type-style';
      }
      case '.msi':
      case '.exe':
      case '.apk': {
        return 'vscode-icons:file-type-light-config';
      }
      default: {
        return 'vscode-icons:default-file';
      }
    }
  }
}
/**
 * @description 判断是否为windows系统
 * @return 是否为windows系统，返回文件路径分隔符
 */
export const splitChar = import.meta.env.MODE === 'development' ? '\\' : '/';
/**
 * 生成指定格式版本号，遇10进1
 * @param version 版本号
 * @returns v0.0.0.1
 */
export function generateVersion(version = 0) {
  const v = [0, 0, 0, version];
  v[2] = Math.floor((v[3] as number) / 10);
  v[3] = (v[3] as number) % 10;
  return `V${v.join('.')}`;
}

export function getNetworkIp(ver = 4) {
  const osType = os.type(); // 系统类型
  const netInfo = os.networkInterfaces();
  let ip = '';
  if (osType === 'Windows_NT') {
    for (const dev in netInfo) {
      // win7的网络信息中显示为本地连接，win10显示为以太网
      if ((dev === '本地连接' || dev === '以太网') && netInfo[dev]) {
        for (let j = 0; j < netInfo[dev].length; j++) {
          if (
            netInfo[dev] &&
            netInfo[dev][j] &&
            netInfo[dev][j]?.family === `IPv${ver}`
          ) {
            ip = netInfo[dev][j]?.address || 'localhost';
            break;
          }
        }
      }
    }
  } else if (osType === 'Linux') {
    const eth0 = netInfo.eth0 ? netInfo.eth0[0] : { address: 'localhost' };
    ip = (eth0 as os.NetworkInterfaceInfo).address || 'localhost';
  }
  return ip;
}
/**
 * 去除UTF-8字符串
 * @param str 字符串
 * @returns 无UTF-8字符串
 */
export const removeNonUtf8Chars = (str: string): string => {
  // 去除所有非标准Unicode字符
  return str.replaceAll(/[\u{D800}-\u{DFFF}]/gu, '').trim();
};
